/**
 * Title: SearchActivity.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2013-11-24 上午11:31:44
 * Version 1.0
 */
package com.luoxudong.phonenumber.ui.activities;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.luoxudong.phonenumber.R;
import com.luoxudong.phonenumber.logic.common.value.SearchType;
import com.luoxudong.phonenumber.logic.search.vo.SearchCondition;
import com.luoxudong.phonenumber.logic.search.vo.SearchItem;
import com.luoxudong.phonenumber.ui.adapters.SearchListAdapter;
import com.luoxudong.phonenumber.ui.common.listener.OnClickAvoidForceListener;
import com.luoxudong.phonenumber.ui.common.listener.OnItemClickAvoidForceListener;
import com.luoxudong.phonenumber.ui.widgets.titlebar.ITitleBarOnClickListener;
import com.luoxudong.phonenumber.ui.widgets.titlebar.TitleBarView;

/** 
 * ClassName: SearchActivity
 * Description:搜索tab
 * Create by 罗旭东
 * Date 2013-11-24 上午11:31:44
 */
public class SearchActivity extends AbsSubActivity{
	private SearchListAdapter adapter = null;
	private ListView searchListView = null;
	private TitleBarView titleBarView = null;
	private List<SearchItem> searchItems = null;
	private int[] numberButtonRess = new int[]{R.id.btn_number_1, R.id.btn_number_2, R.id.btn_number_3, R.id.btn_number_4, R.id.btn_number_5, 
			 								   R.id.btn_number_6, R.id.btn_number_7, R.id.btn_number_8, R.id.btn_number_9, R.id.btn_number_10, R.id.btn_number_11};
	private SearchCondition condition = new SearchCondition();
	
	
	@Override
	protected int getContentViewLayoutResId() {
		return R.layout.search_layout;
	}

	@Override
	protected void onCreateFindView(Bundle savedInstanceState) {
		titleBarView = (TitleBarView)findViewById(R.id.title_bar);
		searchListView = (ListView)findViewById(R.id.lv_search_list);
		
		titleBarView.setTitle(R.string.search);
		titleBarView.changeMode(TitleBarView.MODEL_SEARCH);
	}

	@Override
	protected void onCreateAddListener(Bundle savedInstanceState) {
		searchListView.setOnItemClickListener(onItemClickListener);
		titleBarView.setTitleBarOnClickListener(titleBarOnClickListener);
		
		for (int btnId : numberButtonRess)
		{
			findViewById(btnId).setOnClickListener(onClickListener);
			findViewById(btnId).setOnLongClickListener(onLongClickListener);
		}
	}

	@Override
	protected void onCreateInitData(Bundle savedInstanceState) {
		String[] searchNames = getResources().getStringArray(R.array.search_item);
		searchItems = new ArrayList<SearchItem>();
		if (searchNames != null)
		{
			SearchItem searchItem = null;
			int nIndex = 0;
			for (String searchName : searchNames)
			{
				searchItem = new SearchItem();
				searchItem.setId(nIndex);
				searchItem.setSearchName(searchName);
				searchItems.add(searchItem);
				nIndex++;
			}
		}
		
		adapter = new SearchListAdapter(this, searchItems);
		searchListView.setAdapter(adapter);
	}

	@Override
	protected void onCreateTaskLoadData() {
		
	}

	@Override
	protected void onCreateTaskAddView() {
		
	}

	@Override
	protected void refreshUI(Object obj) {
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		SearchItem searchItem = null;
		
		if (data != null)
		{
			searchItem = (SearchItem)data.getSerializableExtra("searchItem");
		}
		
		if (searchItem == null)
		{
			return;
		}
		
		TextView textView = (TextView)searchListView.getChildAt(requestCode).findViewById(R.id.tv_selected_value);
		
		if (textView != null && searchItem != null)
		{
			textView.setText(searchItem.getSearchName());
		}
		
		if (requestCode ==  SearchType.area.getValue())//地区
		{
			condition.setAreaId(searchItem.getId());
		}
		else if (requestCode ==  SearchType.brand.getValue())
		{
			condition.setBrandId(searchItem.getId());
		}
		else if (requestCode ==  SearchType.packa.getValue())
		{
			condition.setPackaId(searchItem.getId());
		}
		else if (requestCode ==  SearchType.price.getValue())
		{
			condition.setPriceId(searchItem.getId());
		}
		else if (requestCode ==  SearchType.endian.getValue())
		{
			condition.setEndianId(searchItem.getId());
		}
		else if (requestCode ==  SearchType.birthday.getValue())
		{
			condition.setBirthdayId(searchItem.getId());
		}
		else if (requestCode ==  SearchType.auspicious.getValue())
		{
			condition.setAuspiciouId(searchItem.getId());
		}
		else if (requestCode ==  SearchType.numberSegment.getValue())
		{
			if (searchItem.getId() != -1)
			{
				condition.setNumberSegment(searchItem.getSearchName());
			}
			
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
		{
			exitApp();
			return true;
			
		}
		return super.onKeyDown(keyCode, event);
	}
	
	public void onBackPressed() {
		exitApp();
	};
	
	private String getPhoneNumber()
	{
		StringBuilder stringBuilder = new StringBuilder();
		for (int textViewRes : numberButtonRess)
		{
			TextView textView = (TextView)findViewById(textViewRes);
			stringBuilder.append(getNumber(textView));
		}
		
		return stringBuilder.toString();
	}
	
	private String getNumber(TextView textView)
	{
		if (textView == null || textView.getText() == null || "".equals(textView.getText()))
		{
			return "*";
		}
		else
		{
			return textView.getText().toString();
		}
	}
	
	private void search()
	{
		condition.setPhoneNumber(getPhoneNumber());
		
		Intent intent = new Intent(this, SearchResultListActivity.class);
		intent.putExtra("condition", condition);
		startNewActivity(intent);
		
	}
	
	private OnItemClickAvoidForceListener onItemClickListener = new OnItemClickAvoidForceListener() {
		
		@Override
		public void onItemClickAvoidForce(AdapterView<?> parent, View view, int position, long id) {
			Intent intent = new Intent(SearchActivity.this, SelectSearchItemActivity.class);
			intent.putExtra("flag", position);
			startActivityForResult(intent, position);
		}
	};
	
	private OnClickAvoidForceListener onClickListener = new OnClickAvoidForceListener() {
		
		@Override
		public void onClickAvoidForce(View v) {
			Button btn = null;
			
			if (v instanceof Button)
			{
				btn = ((Button)v);
			}
			switch (v.getId()) {
			case R.id.btn_number_2:
			case R.id.btn_number_3:
			case R.id.btn_number_4:
			case R.id.btn_number_5:
			case R.id.btn_number_6:
			case R.id.btn_number_7:
			case R.id.btn_number_8:
			case R.id.btn_number_9:
			case R.id.btn_number_10:
			case R.id.btn_number_11:
				String currentNumTxt = btn.getText().toString();
				int currentNum = -1;//-1表示空
				
				if (currentNumTxt != null && !"".equals(currentNumTxt))
				{
					currentNum = Integer.parseInt(currentNumTxt);
				}
				
				int num = -1;
				
				if (currentNum < 9) {
					num = (currentNum + 1) % 10;
				}
				
				if (num == -1)
				{
					btn.setText("");
				}
				else
				{
					btn.setText(String.valueOf(num));
					
				}
				break;

			default:
				break;
			}
		}
	};
	
	private OnLongClickListener onLongClickListener = new OnLongClickListener() {
		
		@Override
		public boolean onLongClick(View v) {
			if (v instanceof Button)
			{
				switch (v.getId()) {
				case R.id.btn_number_2:
				case R.id.btn_number_3:
				case R.id.btn_number_4:
				case R.id.btn_number_5:
				case R.id.btn_number_6:
				case R.id.btn_number_7:
				case R.id.btn_number_8:
				case R.id.btn_number_9:
				case R.id.btn_number_10:
				case R.id.btn_number_11:
					((Button)v).setText("");
					return true;
				default:
					break;
				}
				
			}
			return false;
		}
	};
	
	private ITitleBarOnClickListener titleBarOnClickListener = new ITitleBarOnClickListener() {
		
		@Override
		public void onRightClick(View v) {
			search();//搜索
		}
		
		@Override
		public void onLeftClick(View v) {
			// TODO Auto-generated method stub
			
		}
	};
	
}
